#!/bin/csh
set convgrd = 0
if ( $convgrd == 1) then
foreach i (`find . -name "MG*.txt"`)
    ##############  UTM to GEOGRAPHICAL coordiante ####################################
    set ZONE = 19
    set MERIDIAN = `echo $ZONE | awk '{print int((('$ZONE'*6)-180)-3)}'`
    set UTM_REGION = `echo $MERIDIAN| awk '{printf("%i/%i/%i/%i\n", $1-3,$1+3,-90,0)}' `
    #echo MERIDIAN $MERIDIAN 
    #echo UTM_REGION $UTM_REGION
    #echo "$i"
    set Name = `echo $i | awk -F "/" '{print $2}' |awk -F "_" '{print $1}'`
    #echo $Name
    set Latlong = "$Name".latlong
    set Input = "$Name".input
    set Annotation = "$Name".annotation
    set Coord = "$Name".coord
    awk '{if(NR>1) printf("%.12lg %.12lg %d \n",$2,$3,$1)}' "$i" | \
    gmt mapproject -V -I -Ju$ZONE/1:1 -R$UTM_REGION -C0/0 -F | \
    awk '{printf("%d %.12lg %.12lg \n",$3,$1,$2)}' > $Latlong 
    awk '{print $2,$3}'  $Latlong > $Input
    awk 'NR%2000==1{print $2,$3,$1}' $Latlong  > $Annotation
    awk 'NR%2000==1{print $2,$3}' $Latlong  > $Coord
end
exit		 
endif
set in_grd="../North-Chile-MltBeam.grd"
set in_shadow="tmp.grd"

set ps="afar.ps"

set font="12p,31,black"
set font_title="15p,29,black"
set R=-71.9/-70.2/-21.3/-18.8

set J=M16c

gmtset MAP_FRAME_TYPE=plain \
       FONT_ANNOT_PRIMARY=$font \
       FONT_LABEL=$font \
       FONT_TITLE=$font_title \
       FORMAT_GEO_MAP=dddF

# 製作輸入檔
# grdcut ../ETOPO1_Bed_g_gmt4.grd -R25/55/-5/25 -G$in_grd
#grdgradient $in_grd -G$in_shadow -A100 -N10
cat << EOF > oceanic.cpt
-9000   0   0   0  -6000  78 176 208
-6000  78 176 208  -3000 126 238 179
-3000 126 238 179 0 255 255 255
B   0   0   0
F 255 255 255
N 255 255 255
EOF

#gmt grdcut $in_grd -G25cut.grd -R-71.56/-20.41/-70.45/-19.60+r 
#gmt grdgradient $in_grd -Nt1 -A60 -Gintens.grd -V
psxy -R0/1/0/1 -JX1c -T -K -P > $ps
grdview $in_grd -R$R/-9000/0 -JM12c -JZ2c -Qs -Coceanic.cpt -p190/20 -N-9000+g150/150/150 -Iintens.grd -Bx0.5 -By0.5 -X3 -Y14c -O -K >> $ps

foreach v (04 23 25 17 13 16A 15 11 32) #add required lines here
psxyz seafloor_$v.txt -R -JM12c -JZ2c -p190/20 -Sp0.1p -O -K >> $ps #plot line
awk 'NR%2000==1{print $1,$2,$3}' seafloor_$v.txt > seafloor_cmp_$v.txt #cmp dots
psxyz seafloor_cmp_$v.txt -R -JM12c -JZ2c -p190/20 -W0.03c,red -Sc-0.05c -O -K >> $ps #plot cmp dots
awk '{print $3}' MGL1610MC$v.annotation > tmp.txt #cmp names
paste seafloor_cmp_$v.txt tmp.txt > cmp_num_$v.txt #cmp texts with location
pstext cmp_num_$v.txt -R -JM12c -JZ2c -p190/20 -F+a-6+f3p,Bookman-Demi -D0.3c/0c -N -Z -O -K >> $ps
end
psxy -R0/1/0/1 -JX1c -O -T >> $ps

psconvert $ps -A -P -Tg

